#include<stdio.h>
#include<unistd.h>
#include<string.h>
int main()
{
  int pipefd[2]={0};
  if(pipe(pipefd)!=0){
    perror("pipe perror!");
    return 1;
  }

  //pipefd[]
  //0：读取
  //1：写入
  printf("pipefd[0]:%d\n",pipefd[0]);
  printf("pipefd[1]:%d\n",pipefd[1]);

  //父进程读取，子进程写入
  if(fork()==0){
    //子进程
    close(pipefd[0]);
    const char *msg="hello tao!\n";
    while(1) {
      //pipe只要里面有缓冲区，就会一直写入
      write(pipefd[1],msg,strlen(msg));
      //sleep(1);
    }
  }
  //父进程
  close(pipefd[1]);
  while(1){
    sleep(1);
    char buffer[64];
    //只要有数据就一直读取
   ssize_t s= read(pipefd[0],buffer,sizeof(buffer)-1);//read ==0 子进程关闭文件描述符
   if(s==0){
     break;
   }
  else if(s>0){
    buffer[s]=0;
    printf("父进程:子进程在说%s\n",buffer);

   }
   else {
     break;
   }

  }

  return 0;
}
